<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: Dean <zxxjjforever@163.com>
// +----------------------------------------------------------------------
// API 模板文件，可以复制
namespace api\home\controller;

use think\Db;
use think\Validate;
use cmf\controller\RestBaseController;
use api\home\controller\TableController;
use api\resource\controller\TablesController;

class WordController extends RestBaseController
{
    public function index()
    {

        $validate = new Validate([
            'pro_id' => 'require',
//            'biao_id'       => 'require',
        ]);

        $validate->message([
            'pro_id.require' => '请输入项目id!',
//            'biao_id.require' => '请输入第几表!',

        ]);

        $data = $this->request->param();
        if (!$validate->check($data)) {
            $this->error($validate->getError());
        }
        //查出关联的表数组
        $project_type_arr = Db::view('project_type', 'l_id,name')
            ->view('project', 'id,company_name', 'project.project_type_id=project_type.id')
            ->where('project.id', '=', $data['pro_id'])
            ->find();
        $project_type_data = $project_type_arr;

        if (!$project_type_arr['l_id']) $this->error("服务器繁忙!");
        $project_type_arr = explode(',', $project_type_arr['l_id']);

        //查出项目名字 和 类型名字
        $name =  $project_type_data['name'];
        $company_name = $project_type_data['company_name'];



        $findFile = Db::name("word")
            ->where('pro_id', $data['pro_id'])
            ->field('wordname')
            ->order('id desc')
            ->find();


        if (!empty($findFile)) {
            $this->success("导出成功!", 'http://word1.cn1.utools.club'.DS.$findFile['wordname']);
        }


        $table = new TableController();
        $html = '';

        foreach ($project_type_arr as $v) {

            $html_code = $table->index($data['pro_id'], $v);

            $html .= $html_code.'<br>';
        }



        $this->start();

//        $wordtime = $company_name.$name;
        $wordtime = time();

        $wordname = ROOT_PATH . 'public' . DS . 'upload' . DS . $wordtime .".doc";
        echo $html;

        $this->save($wordname);

        ob_flush();//每次执行前刷新缓存
        flush();
        $wordname = 'upload' . DS . $wordtime .".doc";

        $word_data['wordname'] = $wordname;
        $word_data['biao_id'] = '';
        $word_data['pro_id'] = $data['pro_id'];
        Db::name('word')->insertGetId($word_data);

        $this->success("导出成功!", 'http://word1.cn1.utools.club'.DS.$wordname);
    }

    public function start()
    {
        ob_start();
        echo '<html xmlns:o="urn:schemas-microsoft-com:office:office"  xmlns:w="urn:schemas-microsoft-com:office:word"  xmlns="http://www.w3.org/TR/REC-html40">
          <head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/><xml><w:WordDocument><w:View>Print</w:View></xml></head><body>';
    }


    public function save($path)
    {
        echo "</html>";
        $data = ob_get_contents();
        ob_end_clean();
        $this->wirtefile($path, $data);
    }

    function wirtefile($fn, $data)
    {
        $fp = fopen($fn, "wb");
        fwrite($fp, $data);
        fclose($fp);
    }


}
